package com.xinbochuang.machine.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.MacRoomResource;
import com.xinbochuang.machine.admin.domain.MacRoomResourceCount;
import com.xinbochuang.machine.admin.domain.MacRoomResourceView;
import com.xinbochuang.machine.admin.domain.macRoom.MacRoom;
import com.xinbochuang.machine.admin.domain.system.SysDictData;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.MacRoomResourceViewMapper;
import com.xinbochuang.machine.admin.mapper.system.SysDictDataMapper;
import com.xinbochuang.machine.admin.service.IMacRoomResourceViewService;
import com.xinbochuang.machine.admin.service.macRoom.IMacRoomService;
import com.xinbochuang.machine.admin.service.system.ISysDeptService;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄晓鹏
 * @date 2020-12-11
 */
@Service
public class MacRoomResourceViewServiceImpl extends ServiceImpl<MacRoomResourceViewMapper, MacRoomResourceView> implements IMacRoomResourceViewService {

    @Resource
    private TokenService tokenService;

    @Resource
    private MacRoomResourceViewMapper macRoomResourceViewMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private ISysDeptService deptService;

    @Resource
    private IMacRoomService macRoomService;

    @Override
    public TableDataInfo selectMacRoomResourceData(MacRoomResource macRoomResource) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
////        if(user==null){
//          SysUser  user=new SysUser();
//            user.setDeptId(6);
////        }
        List<Map<String, Object>> mapList = macRoomResourceViewMapper.selectMacRoomResourceData(macRoomResource.getMacRoomId(), user.getDeptId() + "", macRoomResource.getContent1());
        Map<String, Map<String, Object>> macRoomMap = new HashMap<>();
        List<String> ids = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            if (!macRoomMap.containsKey(map.get("MACID"))) {
                Map<String, Object> macRoom = new HashMap<>();
                macRoom.put("macId", map.get("MACID"));
                macRoom.put("macName", map.get("MACNAME"));
                macRoomMap.put(map.get("MACID") + "", macRoom);
            }
            if (!ids.contains(map.get("MACID"))) {
                ids.add(map.get("MACID") + "");
            }
            macRoomMap.get(map.get("MACID") + "").put(map.get("RESOURCETYPE") + "", map.get("NUM"));
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (String id : ids) {
            result.add(macRoomMap.get(id));
        }
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDictData::getDictType, "mac_resource_type").orderByAsc(SysDictData::getDictSort);
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectList(queryWrapper);
        for (Map<String, Object> map : result) {
            for (SysDictData sysDictData : sysDictDataList) {
                if (!map.containsKey(sysDictData.getDictLabel())) {
                    map.put(sysDictData.getDictLabel(), "0");
                }
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(result.size());
        if (result.size() == 0) {
            return tableDataInfo;
        } else if (result.size() >= macRoomResource.getPageSize() * macRoomResource.getPageNum() - 1) {
            tableDataInfo.setRows(result.subList((macRoomResource.getPageNum() - 1) * macRoomResource.getPageSize(), macRoomResource.getPageNum() * macRoomResource.getPageSize() - 1));
        } else {
            tableDataInfo.setRows(result.subList((macRoomResource.getPageNum() - 1) * macRoomResource.getPageSize(), result.size()));
        }
        return tableDataInfo;
    }

    @Override
    public IPage<MacRoomResourceView> listByPage(MacRoomResourceView macRoomResourceView) {

        QueryWrapper<MacRoomResourceView> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(macRoomResourceView.getResourceType())) {
            queryWrapper.lambda().eq(MacRoomResourceView::getResourceType, macRoomResourceView.getResourceType());
        }
        if (StringUtils.isNotBlank(macRoomResourceView.getMacRoomId())) {
            queryWrapper.lambda().eq(MacRoomResourceView::getMacRoomId, macRoomResourceView.getMacRoomId());
        }
        if (StringUtils.isNotBlank(macRoomResourceView.getTitle())) {
            queryWrapper.lambda().like(MacRoomResourceView::getTitle, macRoomResourceView.getTitle());
        }
        if (StringUtils.isNotBlank(macRoomResourceView.getCode())) {
            queryWrapper.lambda().like(MacRoomResourceView::getCode, macRoomResourceView.getCode());
        }

        return page(new Page<>(macRoomResourceView.getPageNum(), macRoomResourceView.getPageSize()), queryWrapper);
    }

    @Override
    public Map selectMacRoomResourceDataByCharts(MacRoomResource macRoomResource) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
////        if(user==null){
//          SysUser  user=new SysUser();
//            user.setDeptId(6);
////        }
        List<Map<String, Object>> mapList = macRoomResourceViewMapper.selectMacRoomResourceData(macRoomResource.getMacRoomId(), user.getDeptId() + "", macRoomResource.getContent1());
        Map<String, Map<String, Object>> macRoomMap = new HashMap<>();
        List<String> ids = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            if (!macRoomMap.containsKey(map.get("MACID"))) {
                Map<String, Object> macRoom = new HashMap<>();
                macRoom.put("macId", map.get("MACID"));
                macRoom.put("macName", map.get("MACNAME"));
                macRoomMap.put(map.get("MACID") + "", macRoom);
            }
            if (!ids.contains(map.get("MACID"))) {
                ids.add(map.get("MACID") + "");
            }
            macRoomMap.get(map.get("MACID") + "").put(map.get("RESOURCETYPE") + "", map.get("NUM"));
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (String id : ids) {
            result.add(macRoomMap.get(id));
        }
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDictData::getDictType, "mac_resource_type").orderByAsc(SysDictData::getDictSort);
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectList(queryWrapper);
        List<String> name = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        for (SysDictData sysDictData : sysDictDataList) {
            name.add(sysDictData.getDictLabel());
            int num = 0;
            for (Map<String, Object> map : result) {
                if (map.containsKey(sysDictData.getDictLabel())) {
                    num += Integer.valueOf(map.get(sysDictData.getDictLabel()) + "");
                }
            }
            value.add(num);

        }

        for (Map<String, Object> map : result) {
            for (SysDictData sysDictData : sysDictDataList) {
                if (!map.containsKey(sysDictData.getDictLabel())) {
                    map.put(sysDictData.getDictLabel(), "0");
                }
            }
        }
        List<Map<String, Object>> finalResult = new ArrayList<>();
        for (int i = 0; i < name.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name.get(i));
            map.put("value", value.get(i));
            finalResult.add(map);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("data", finalResult);
        data.put("name", name);
        return data;
    }

    /**
     * 机房资源统计
     *
     * @return 结果
     */
    @Override
    public List<MacRoomResourceCount> resourceCount() {
        QueryWrapper<MacRoom> queryWrapper = new QueryWrapper<>();
        List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        if (!deptIds.isEmpty()) {
            queryWrapper.lambda().in(MacRoom::getDeptId, deptIds);
        }
        List<MacRoom> list = macRoomService.list(queryWrapper);
        List<String> macIds = new ArrayList<>();
        for (MacRoom macRoom : list) {
            macIds.add(macRoom.getUuid());
        }
        return macRoomResourceViewMapper.selectResourceCount(macIds);
    }

    /**
     * 机房资源统计数量
     *
     * @return 结果
     */
    @Override
    public Long resourceTotal() {
        QueryWrapper<MacRoom> queryWrapper = new QueryWrapper<>();
        List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        if (!deptIds.isEmpty()) {
            queryWrapper.lambda().in(MacRoom::getDeptId, deptIds);
        }
        List<MacRoom> list = macRoomService.list(queryWrapper);
        List<String> macIds = new ArrayList<>();
        for (MacRoom macRoom : list) {
            macIds.add(macRoom.getUuid());
        }
        return macRoomResourceViewMapper.selectResourceTotal(macIds);
    }
}
